#include "campus_whole_server.h"
#include "../log/campus_logger.h"

CampusLoginServerObserver* CampusWholeServer::login_observer_ = nullptr;
CampusPersonInfoObserver* CampusWholeServer::person_info_observer_ = nullptr;
CampusClubInfoObserver* CampusWholeServer::club_info_observer_ = nullptr;
CampusLiveInfoObserver* CampusWholeServer::live_observer_ = nullptr;


CampusWholeServer::CampusWholeServer(int port):
    CampusWssServer(port)
{
}

CampusWholeServer::~CampusWholeServer()
{
}

void CampusWholeServer::on_open(server *s, websocketpp::connection_hdl hdl)
{
    CampusLogger::info("login_server: on_open");
    CampusWssServer::on_open(s,hdl);
}

void CampusWholeServer::on_close(server *s, websocketpp::connection_hdl hdl)
{
    CampusLogger::info("login_server: on_close");
    if(live_observer_ != nullptr){
        live_observer_->handleLiveLeave(hdl);
    }
    if(login_observer_ != nullptr){
        login_observer_->handleHdlClose(hdl);
    }
    CampusWssServer::on_close(s,hdl);
}

void CampusWholeServer::on_message(server *s, websocketpp::connection_hdl hdl, message_ptr msg)
{
    std::string msg_str = msg->get_payload();
    CampusLogger::info("on_message: {}", msg_str);

    json JMsg = json::parse(msg_str);
    std::string cmd = JMsg["cmd"];

    if(cmd == SIGNAL_TYPE_LOGIN){
        login_observer_->handleLogin(hdl,JMsg);
    }
    else if(cmd == SIGNAL_TYPE_LOGIN_SALT){
        login_observer_->handleLoginSalt(hdl,JMsg);
    }
    else if(cmd == SIGNAL_TYPE_REGISTER){
        login_observer_->handleRegister(hdl,JMsg);
    }
    else if(cmd == SIGNAL_TYPE_LOGOUT){
        login_observer_->handleLogout(hdl,JMsg);
    }
    else if(cmd == SIGNAL_TYPE_PERSON_INFO){
        person_info_observer_->handlePersonInfo(hdl,JMsg);
    }
    else if(cmd == SIGNAL_TYPE_PERSON_INFO_MODIFY){
        person_info_observer_->handlePersonInfoModify(hdl,JMsg);
    }
    else if(cmd == SIGNAL_TYPE_CREATE_CLUB){
        club_info_observer_->handleClubCreate(hdl,JMsg);
    }
    else if(cmd == SIGNAL_TYPE_SEARCH_CLUB){
        club_info_observer_->handleClubSearch(hdl,JMsg);
    }
    else if(cmd == SIGNAL_TYPE_JOIN_CLUB){
        club_info_observer_->handleClubJoin(hdl,JMsg);
    }
    else if(cmd == SIGNAL_TYPE_CLUB_INFO){
        club_info_observer_->handleClubInfo(hdl,JMsg);
    }
    else if(cmd == SIGNAL_TYPE_GROUP_INFO){
        club_info_observer_->handleClubGroup(hdl,JMsg);
    }
    else if(cmd == SIGNAL_TYPE_GROUP_CHAT){
        club_info_observer_->handleGroupChat(hdl,JMsg);
    }
    else if(cmd == SIGNAL_TYPE_LIVE_CHAT){
        live_observer_->handleLiveChat(hdl,JMsg);
    }
    else if(cmd == SIGNAL_TYPE_LIVE_JOIN){
        live_observer_->handleLiveJoin(hdl,JMsg);
    }
    else if(cmd == SIGNAL_TYPE_LIVE_LEAVE){
        live_observer_->handleLiveLeave(hdl);
    }
}

void CampusWholeServer::on_fail(websocketpp::connection_hdl hdl)
{
    CampusLogger::info("login_server: on_fail");
    CampusWssServer::on_fail(hdl);
}


CampusWholeServer *CampusWholeServer::getInstance()
{
    static CampusWholeServer* instance_ = new CampusWholeServer();
    return instance_;
}

void CampusWholeServer::setLoginObserver(CampusLoginServerObserver *observer)
{
    if(observer == nullptr){
        CampusLogger::warn("CampusWholeServer::setLoginObserver, login_observer_ is nullptr");
        return;
    }
    else if(login_observer_ != nullptr){
        CampusLogger::warn("CampusWholeServer::setLoginObserver, login_observer_ is not nullptr");
        return;
    }

    login_observer_ = observer;
}

void CampusWholeServer::setPersonInfoObserver(CampusPersonInfoObserver *observer)
{
    if(observer == nullptr){
        CampusLogger::warn("CampusWholeServer::setPersonInfoObserver, observer is nullptr");
        return;
    }
    else if(person_info_observer_ != nullptr){
        CampusLogger::warn("CampusWholeServer::setPersonInfoObserver, person_info_observer_ is not nullptr");
        return;
    }

    person_info_observer_ = observer;
}

void CampusWholeServer::setClubInfoObserver(CampusClubInfoObserver *observer)
{
    if(observer == nullptr){
        CampusLogger::warn("CampusWholeServer::setClubInfoObserver, observer is nullptr");
        return;
    }
    else if(club_info_observer_ != nullptr){
        CampusLogger::warn("CampusWholeServer::setClubInfoObserver, club_info_observer_ is not nullptr");
        return;
    }

    club_info_observer_ = observer;
}

void CampusWholeServer::setLiveObserver(CampusLiveInfoObserver *observer)
{
    if(observer == nullptr){
        CampusLogger::warn("CampusWholeServer::setLiveObserver, observer is nullptr");
        return;
    }
    else if(live_observer_ != nullptr){
        CampusLogger::warn("CampusWholeServer::setLiveObserver, live_observer_ is not nullptr");
        return;
    }
    
    live_observer_ = observer;
}
